package com.sdkj.recruit.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IMerchantManagementDao;
import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.model.Merchant;
import com.sdkj.recruit.model.MerchantLog;
import com.sdkj.recruit.model.MerchantTask;
import com.sdkj.recruit.model.Paginate;
import com.sdkj.recruit.model.SmallNoTask;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.util.StringUtil;


@Persistent
@Service("merchantDao")
public class MerchantManagementDao implements IMerchantManagementDao {

	@Autowired
	private IMongodbDAO dao;

	@Override
	public List<Merchant> queryMerchantList(int currentPage, int pageSize, String merchantKey) {

		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "createDate");
		orders.add(order);
		
		if(StringUtil.isNotEmptyString(merchantKey)){
			return dao.getPOList(Merchant.class, new Paginate(currentPage, pageSize), orders,
					QueryParamCreator.like("merchantName", merchantKey));
		}
		else{
			return dao.getPOList(Merchant.class, new Paginate(currentPage, pageSize), orders,QueryParamCreator.isNotNull("merchantId"));
		}
		
	}

	@Override
	public long queryMerchantCount(int currentPage, int pageSize, String merchantKey) {
		if(StringUtil.isNotEmptyString(merchantKey)){
			return dao.getRowCount(Merchant.class, QueryParamCreator.like("merchantName", merchantKey));
		}
		else{
			return dao.getRowCount(Merchant.class, QueryParamCreator.isNotNull("merchantId"));
		}
		
	}

	@Override
	public Merchant queryMerchantById(String merchantId) {
		return dao.getPO(Merchant.class, QueryParamCreator.eq("merchantId", merchantId));
	}

	@Override
	public void saveMerchantTask(MerchantTask mTask) {
		dao.saveOrUpdatePO(mTask);
	}

	@Override
	public void saveMerchant(Merchant merchant) {
		dao.saveOrUpdatePO(merchant);
	}

	@Override
	public void saveMerchantLog(MerchantLog log) {
		dao.saveOrUpdatePO(log);
	}

	@Override
	public long queryMerchantTaskById(String merchantId) {
		return dao.getRowCount(MerchantTask.class, QueryParamCreator.eq("merchantId", merchantId));
	}

	@Override
	public void saveSmallNoTask(SmallNoTask mTask) {
		dao.saveOrUpdatePO(mTask);
	}

}
